{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ScrapFly\n",
    "[ScrapFly](https://scrapfly.io/) is a web scraping API with headless browser capabilities, proxies, and anti-bot bypass. It allows for extracting web page data into accessible LLM markdown or text."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Installation\n",
    "Install ScrapFly Python SDK and he required Langchain packages using pip:\n",
    "```shell\n",
    "pip install scrapfly-sdk langchain langchain-community\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.document_loaders import ScrapflyLoader\n",
    "\n",
    "scrapfly_loader = ScrapflyLoader(\n",
    "    [\"https://web-scraping.dev/products\"],\n",
    "    api_key=\"Your ScrapFly API key\",  # Get your API key from https://www.scrapfly.io/\n",
    "    ignore_scrape_failures=True,  # Ignore unprocessable web pages and log their exceptions\n",
    ")\n",
    "\n",
    "# Load documents from URLs as markdown\n",
    "documents = scrapfly_loader.load()\n",
    "print(documents)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ScrapflyLoader also allows passigng ScrapeConfig object for customizing the scrape request. See the documentation for the full feature details and their API params: https://scrapfly.io/docs/scrape-api/getting-started"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.document_loaders import ScrapflyLoader\n",
    "\n",
    "scrapfly_scrape_config = {\n",
    "    \"asp\": True,  # Bypass scraping blocking and antibot solutions, like Cloudflare\n",
    "    \"render_js\": True,  # Enable JavaScript rendering with a cloud headless browser\n",
    "    \"proxy_pool\": \"public_residential_pool\",  # Select a proxy pool (datacenter or residnetial)\n",
    "    \"country\": \"us\",  # Select a proxy location\n",
    "    \"auto_scroll\": True,  # Auto scroll the page\n",
    "    \"js\": \"\",  # Execute custom JavaScript code by the headless browser\n",
    "}\n",
    "\n",
    "scrapfly_loader = ScrapflyLoader(\n",
    "    [\"https://web-scraping.dev/products\"],\n",
    "    api_key=\"Your ScrapFly API key\",  # Get your API key from https://www.scrapfly.io/\n",
    "    ignore_scrape_failures=True,  # Ignore unprocessable web pages and log their exceptions\n",
    "    scrape_config=scrapfly_scrape_config,  # Pass the scrape_config object\n",
    "    scrape_format=\"markdown\",  # The scrape result format, either `markdown`(default) or `text`\n",
    ")\n",
    "\n",
    "# Load documents from URLs as markdown\n",
    "documents = scrapfly_loader.load()\n",
    "print(documents)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
